package com.example.poemtest.index.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.poemtest.R;
import com.example.poemtest.entity.VideoPoetryBean;
import com.example.poemtest.index.entity.PoetryVideo;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * created by JHT on 2024/4/7
 * 诗词大会视频的适配器
 */
public class PoetryVideoAdapter extends BaseAdapter {

    private Context mContext;
    private List<VideoPoetryBean.VideoPoetry> poetryVideoList;
    private int layoutResId;

    public PoetryVideoAdapter(Context mContext, List<VideoPoetryBean.VideoPoetry> poetryVideoList, int layoutResId) {
        this.mContext = mContext;
        this.poetryVideoList = poetryVideoList;
        this.layoutResId = layoutResId;
    }

    @Override
    public int getCount() {
        if (poetryVideoList != null) {
            return poetryVideoList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (poetryVideoList != null) {
            return poetryVideoList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            //加载item的布局控件，生成一个View对象，再给convertView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.poetryVideoImg = convertView.findViewById(R.id.poetry_video_img);
            viewHolder.poetryVideoTitle = convertView.findViewById(R.id.poetry_video_title);
            //将ViewHolder对象同convertView关联起来
            convertView.setTag(viewHolder);
        } else {
            //重复利用converView对象
            //获取到跟当前的converView绑定在一起的ViewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //给控件对象绑定要显示的数据
        String str = poetryVideoList.get(position).getVideoPhotoSrc().toString();
//        Uri uri = Uri.parse(str);
        Log.e("shipin",str.toString());
//        viewHolder.poetryVideoImg.setImageURI(uri);
        Picasso.get().load(str).into(viewHolder.poetryVideoImg);
        viewHolder.poetryVideoTitle.setText(poetryVideoList.get(position).getVideoName());
        return convertView;
    }

    private static class ViewHolder {
        //每一个item的控件
        ImageView poetryVideoImg;
        TextView poetryVideoTitle;
    }

}
